package com.hs.hu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.dto.Constants;
import com.hs.dto.PageBean;
import com.hs.dto.ResultBean;
import com.hs.hu.pojo.Confirm;
import com.hs.hu.pojo.Dispatches;
import com.hs.hu.pojo.Receipt;
import com.hs.hu.mapper.ReceiptMapper;
import com.hs.hu.service.IReceiptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Jiajie Hu
 * @since 2022-06-13
 */
@Service
public class ReceiptServiceImpl extends ServiceImpl<ReceiptMapper, Receipt> implements IReceiptService {

    @Autowired
    private ReceiptMapper receiptMapper;

    @Override
    public ResultBean queryAllReceiptPage(Integer currentPage, Integer pageSize, String orderId, String customerName) {
        orderId = orderId==null || ("").equals(orderId)?"%":orderId;
        customerName = customerName==null || ("").equals(customerName)?"%":customerName;

        PageHelper.startPage(currentPage,pageSize);

        List<Confirm> confirms=receiptMapper.QueryAllReceiptPage(orderId,customerName);

        PageInfo<Confirm> info=new PageInfo<>(confirms);

        List<Confirm> confirmList=info.getList();

        Long total=info.getTotal();
        if(!CollectionUtils.isEmpty(confirmList)){
            PageBean<Confirm> pageBean = new PageBean<>();
            pageBean.setList(confirmList);
            pageBean.setTotal(total.intValue());
            ResultBean resultBean = new ResultBean();
            resultBean.setData(pageBean);
            resultBean.setMsg("successful");
            resultBean.setCode(Constants.SUCCESS_CODE);
            return resultBean;
        }
        return ResultBean.ERROR("failed");
    }

    @Override
    public ResultBean queryAllDispatchPage(Integer currentPage, Integer pageSize, Integer dispatchId, String dispatchCarPlate, String dispatchCarDriver) {
        dispatchId = dispatchId==null ? Integer.valueOf("%") :dispatchId;
        dispatchCarPlate = dispatchCarPlate==null || ("").equals(dispatchCarPlate)?"%":dispatchCarPlate;
        dispatchCarDriver = dispatchCarDriver==null || ("").equals(dispatchCarDriver)?"%":dispatchCarDriver;

        PageHelper.startPage(currentPage,pageSize);

        List<Dispatches> dispatches=receiptMapper.queryAllDispatchPage(dispatchId,dispatchCarPlate,dispatchCarDriver);

        PageInfo<Dispatches> info=new PageInfo<>(dispatches);

        List<Dispatches> dispatchesList = info.getList();
        Long total = info.getTotal();
        if(!CollectionUtils.isEmpty(dispatchesList)){
            PageBean<Dispatches> pageBean = new PageBean<>();
            pageBean.setList(dispatchesList);
            pageBean.setTotal(total.intValue());
            ResultBean resultBean = new ResultBean();
            resultBean.setData(pageBean);
            resultBean.setMsg("successful");
            resultBean.setCode(Constants.SUCCESS_CODE);
            return resultBean;
        }
        return ResultBean.ERROR("failed");
    }


}
